package com.frinder.netty;

import io.netty.handler.codec.marshalling.*;
import org.jboss.marshalling.MarshallerFactory;
import org.jboss.marshalling.Marshalling;
import org.jboss.marshalling.MarshallingConfiguration;

/**
 * Created by kiktech on 2017/8/4.
 * <p>
 * netty 编码/解码器
 */
public class MarshallingCodeCFactory {

    private static final MarshallerFactory MARSHALLER_FACTORY = Marshalling.getProvidedMarshallerFactory("serial");
    private static final MarshallingConfiguration MARSHALLING_CONFIGURATION = new MarshallingConfiguration();
    private static final UnmarshallerProvider UNMARSHALLER_PROVIDER;
    private static final MarshallerProvider MARSHALLER_PROVIDER;
    private static final int MAX_SIZE = 1024 * 1024;

    static {
        MARSHALLING_CONFIGURATION.setVersion(5);
        UNMARSHALLER_PROVIDER = new DefaultUnmarshallerProvider(MARSHALLER_FACTORY, MARSHALLING_CONFIGURATION);
        MARSHALLER_PROVIDER = new DefaultMarshallerProvider(MARSHALLER_FACTORY, MARSHALLING_CONFIGURATION);
    }

    /**
     * 解码器
     *
     * @return
     */
    public static MarshallingDecoder decoder() {
         return new MarshallingDecoder(UNMARSHALLER_PROVIDER, MAX_SIZE);
    }

    /**
     * 编码器
     *
     * @return
     */
    public static MarshallingEncoder encoder() {
        return new MarshallingEncoder(MARSHALLER_PROVIDER);
    }
}
